package com.register.dao;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
import com.register.beans.RegisterBean;

/**
 * Servlet implementation class MemberRegisterServlet
 */
public class MemberRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RegisterBean registerBean = null;
	private RegisterDAO registerDAO = null;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String savePath = request.getServletContext().getRealPath("userImg").replace("\\", "/");
//		String savePath = "userImg";
		System.out.println("savePath = " + savePath);
		File f = new File(savePath);
		if (!f.isFile()) {
			if (!f.isDirectory()) {
				f.mkdir();
			}
		}

		int sizeLimit = 1024 * 1024 * 15;

		MultipartRequest multi = new MultipartRequest(request, savePath, sizeLimit, "utf-8", new DefaultFileRenamePolicy());

		String fileName = multi.getFilesystemName("btnImgFile");
		String btnImgFile = savePath + "/" + fileName;

		registerBean = new RegisterBean();
		registerBean.setName(multi.getParameter("username"));
		registerBean.setId(multi.getParameter("userid"));
		registerBean.setPassWd(multi.getParameter("passwd"));
		registerBean.setEmail(multi.getParameter("email"));
		registerBean.setSex(multi.getParameter("sex"));
		registerBean.setBirthDate(multi.getParameter("birthdate").replace("-", ""));
		registerBean.setCellPhone(multi.getParameter("cellphone"));
		registerBean.setImgPath(fileName);

		registerDAO = new RegisterDAO();
		int insertCnt = 0;
		try {
			insertCnt = registerDAO.insertMember(registerBean);
		} catch (SQLException e) {
			if (!(insertCnt > 0)) {
				File file = new File(btnImgFile);
				if (file.isFile()) {
					file.delete();
				}
			}
			e.printStackTrace();
		}

		if (insertCnt > 0) {
			System.out.println("데이터입력 성공.");
			RegisterBean memberInfo = registerDAO.searchMember(registerBean.getId(), registerBean.getPassWd());
			request.setAttribute("memberInfo", memberInfo);
			request.getRequestDispatcher("registerInfo.forrest").forward(request, response);
		} else {
			f.delete();
			request.setAttribute("insertCnt", "0");
			request.getRequestDispatcher("registerTemplate.forrest").forward(request, response);

		}

	}

}
